﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Log;
using PlanningGIS.ArcGIS;

using ESRI.ArcGIS.esriSystem;
using System.Configuration;
using PlanningGIS.Framework.Controls;
using PlanningGIS.Framework;

namespace PlanningGIS.Entry
{
    /// <summary>
    /// 系统主入口
    /// </summary>
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
           
                try
                {
                   ESRI.ArcGIS.RuntimeManager.BindLicense( ESRI.ArcGIS.ProductCode.EngineOrDesktop, ESRI.ArcGIS.LicenseLevel.GeodatabaseUpdate);
                    AoInitialize _aoInitialize = new AoInitialize();
                    if (_aoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) != esriLicenseStatus.esriLicenseAvailable)
                    {
                        MsgHelper.ShowWarningMsgbox("无法启动高级许可！");
                        return;
                    }
                    _aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
                    esriLicenseStatus s = _aoInitialize.IsExtensionCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeAdvanced, esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
                    if (s == esriLicenseStatus.esriLicenseAvailable)
                    {
                        _aoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
                    }
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    //Application.Run(new MainFramework());
                    //_aoInitialize.Shutdown();

                    if (StartHelper.StartApp())
                    {
                        Application.Run(new FrmMain());
                    }
                    else
                    {
                        Application.Exit();
                    }
                    _aoInitialize.Shutdown();

                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorText(ex);
                    MsgHelper.ShowWarningMsgbox("系统运行异常：" + ex.Message);
                }
                       
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        static bool Update()
        {
            try
            {
                AutoUpdater up = new AutoUpdater();
                if (!up.CheckUpdated()) return false;
                if (MessageBox.Show("检查到系统有更新包，是否更新系统？", "", MessageBoxButtons.YesNo) != DialogResult.Yes) return false;
                up.StartExe();
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
                return false;
            }         
            
        }
        
    }
}
